package practice.string;

public class SequentialWordsNumbers {

    public static String sequentialWordsNumbers(String text){
        if (text.isBlank()) {
            return text;
        }

        StringBuilder sequentialWords = new StringBuilder();
        String blank = " ";

        int pointer = -1;
        int count = 0;

        while (true) {
            sequentialWords.append("(");
            sequentialWords.append(++count);
            sequentialWords.append(") ");

            int next = text.indexOf(blank, pointer + 1);

            if (next > pointer) {
                sequentialWords.append(text, pointer + 1, next + 1);
                pointer = next;
            } else {
                sequentialWords.append(text, pointer + 1, text.length());
                break;
            }
        }

        return sequentialWords.toString();
    }
}
